package railRoad.web.slaves;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import railRoad.Route;
import railRoad.Schedule;
import railRoad.Ticket;
import railRoad.web.slaves.interfaces.ISlaveServlet;
import railRoad.worker.FactoryRW;

public class CancelOrderServlet implements ISlaveServlet {

	public void work(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		Map<?, ?> parameterMap = req.getParameterMap();

		if (parameterMap.containsKey("ticket_id")) {
			int tId = Integer.valueOf(req.getParameterValues("ticket_id")[0]); 

			Ticket ticket = new Ticket();

			ticket = FactoryRW.select(ticket, tId);
			if (ticket != null) {
				Route route = new Route(0, 0);
				
				Schedule schedule = Schedule.getInstance();
				
				route = schedule.getRouteById(ticket.getRoute_id());

				if (route != null) {
					route.returnTicket();

					FactoryRW.delete(ticket);
					FactoryRW.update(route);
				}// if route
			}// if ticket
		}// if parameterMap
		resp.sendRedirect("/pages/history.do");
	}// work

}// CancelOrderServlet
